package com.qf.six.ssm.service.admin.impl;

import com.qf.six.ssm.commen.exception.ServiceException;
import com.qf.six.ssm.commen.request.UserParam;
import com.qf.six.ssm.commen.vo.ResponseCode;
import com.qf.six.ssm.commen.vo.ResponseResult;
import com.qf.six.ssm.entity.User;
import com.qf.six.ssm.mapper.admin.UserMapper;
import com.qf.six.ssm.service.admin.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author ZL
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    /**
    * 登录
    * */
    @Override
    public ResponseResult<Object> selectUser(UserParam param) {
        User user = userMapper.selectUser(param.getUsername());
        if(user != null){
            if(user.getPassword().equals(param.getPassword())){
                return ResponseResult.success(ResponseCode.SUCCESS);
            }else {
                return ResponseResult.error(ResponseCode.PASSWORD_ERROR);
            }
        }
        return ResponseResult.error(ResponseCode.USERNAME_ERROR);
    }
    /**
    * 注册
    * */
    @Override
    public ResponseResult<Object> insertUser(UserParam param) {
        User user = userMapper.selectUser(param.getUsername());
        if (user==null) {
            Integer count = userMapper.insertUser(param);
            if(count == 1){
                return ResponseResult.success(ResponseCode.SUCCESS);
            }
        }else {
            return  ResponseResult.error(ResponseCode.REGISTER_ERROR);
        }
        return ResponseResult.error(ResponseCode.REGISTER_ERROR);
    }
}
